package com.bupt.uchouten.client.listener;

import net.minecraft.client.Minecraft;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.event.TickEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;

import java.util.Optional;

@Mod.EventBusSubscriber(value = Dist.CLIENT)
public class ScreenShakeHandler {

    /**
     * The remaining time for which the screen shake effect will be active.
     */
    private static int screenShakeCounter = 0;
    private static final float SHAKINESS = 0.5f;

    /**
     * Starts the client-side screen shake effect.
     */
    public static void shakeScreen(float intensity) {
//        if(Wizardry.settings.screenShake){
        // Start halfway down
        Optional.ofNullable(Minecraft.getInstance().player).ifPresent(player -> {
                    screenShakeCounter = (int) (intensity / SHAKINESS);
                    player.setXRot(player.getXRot() - intensity * 0.5f);
                }
        );
//        }
    }

    @SubscribeEvent
    public static void onPlayerTickEvent(TickEvent.PlayerTickEvent event) {
        if (event.player == Minecraft.getInstance().player && event.phase == TickEvent.Phase.END) {

//            if(Wizardry.settings.screenShake){
            if (screenShakeCounter > 0) {
                Optional.ofNullable(Minecraft.getInstance().player).ifPresent(player -> {
                            float magnitude = screenShakeCounter * SHAKINESS;
                            player.setXRot(player.getXRot() + (screenShakeCounter % 2 == 0 ? magnitude : -magnitude));
                            screenShakeCounter--;
                        }
                );
            }
//            }else{
//                screenShakeCounter = 0;
//            }
        }
    }

}